package com.bc.trade.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.log4j.Logger;

/**
 * 
 * @author xie
 *
 */
public class Aes {

	private static byte[] AES_KEY;

	private static Logger logger = Logger.getLogger(Aes.class.getName());

	private static Object lock = new Object();
	private static void initAesKey() {

		byte[] local = null;
		synchronized (lock) {

			String[] src = new String[3];
			src[0] = "BDEF65189047614AED78E9BC901EAF27";
			src[1] = "890153BE7AD1FBA981036CB7A5E4BFFE";
			src[2] = "6A4E1BF6CDDD7152638BAEDC1928AEDB";

			local = Util.str2Bcd(src[0]);
			if(src.length > 1) {

				for(int i = 1;i < src.length;i++) {

					byte[] tmp = Util.str2Bcd(src[i]);

					for(int j = 0;j < local.length;j++) {
						local[j] = (byte)(local[j]^tmp[j]);
					}
				}
			}
		}

		if (local != null) {
			AES_KEY = local;
		}

	}

	/**
	 * ʹ��aes���ַ������м���
	 * @param str
	 * @return
	 */
	public static String encrypt(String str) {

		if (Strings.isNullOrEmpty(str)) {
			return "";
		}
		try {
			if (AES_KEY == null || AES_KEY.length != 16) {
				initAesKey();
			}

			return Util.bcdBytes2Str(encrypt(str.getBytes(), AES_KEY));
		} catch (Exception e) {
			logger.error(Strings.toString(e));
		}

		return str;
	}

	/**
	 * ʹ��aes���ַ������н���
	 * @param str
	 * @return
	 */
	public static String decrypt(String str) {

		if (Strings.isNullOrEmpty(str)) {
			return "";
		}

		try {
			if (AES_KEY == null || AES_KEY.length != 16) {
				initAesKey();
			}

			return new String(decrypt(Util.str2Bcd(str), AES_KEY));
		} catch (Exception e) {
			logger.error(Strings.toString(e));
		}

		return str;
	}

	private static byte[] encrypt(byte[] data, byte[] keyByte) throws Exception {

		Cipher cipher = Cipher.getInstance("AES");
		SecretKey key = new SecretKeySpec(keyByte, "AES");
		cipher.init(Cipher.ENCRYPT_MODE, key);

		return cipher.doFinal(data);

	}

	private static byte[] decrypt(byte[] data, byte[] keyByte) throws Exception {

		Cipher cipher = Cipher.getInstance("AES");
		SecretKey key = new SecretKeySpec(keyByte, "AES");
		cipher.init(Cipher.DECRYPT_MODE, key);

		return cipher.doFinal(data);
	}

	/**
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		//        String end = AES.encrypt("i want to XXXx you������һ��");
		//        System.out.println(enc);
		//        System.out.println(AES.decrypt(enc));
		//System.out.println(Aes.decrypt("F5634D02D5C58DC7340DC7160B4463FA"));
		//System.out.println(Aes.decrypt("EA70B77804851DC304357505DFE965886F6A57817D3C2B7002984B1CEF1A8FA2"));
		//System.out.println("A003BABB08C2D6F1E7A84B25F918A3F09E185C4F38C5A464EFED95894A2C4722".length());
		//System.out.println(Aes.encrypt("12345678"));
		System.out.println(Aes.encrypt("BT{wkE^D"));
		System.out.println(Aes.decrypt("3323167343F8142AF91074D542DBD0B3"));
//		System.out.println(Aes.decrypt("0019CAA1B20039818AE43580F5F9317ADD0FC3AE16246A28AC3A3FD0C888E829"));
//		System.out.println(Aes.decrypt("001C30C3E6CE511ACE81BDEEB7FD12190CD535CF203E359E73AB0C6DDB57C65A"));
//		System.out.println(Aes.decrypt("0021ACA88108EA17D1E0722C9F88D5139EF9E18E63B43C5ACE8CB91974EAA461"));
//		System.out.println(Aes.decrypt("0029FE41903E9B4A5D94DF946249B49DDD0FC3AE16246A28AC3A3FD0C888E829"));
//		System.out.println(Aes.decrypt("002C93C445E2C8FC4E4B7C46F2A660766C000FC088147C4BDAE9A13A278F2E2A"));
//		System.out.println(Aes.decrypt("0033454B08766A41FB9BC57CC01488C4DD0FC3AE16246A28AC3A3FD0C888E829"));

		System.out.println("6214831206599216".hashCode()%99);
		System.out.println(Aes.encrypt("6214831206599216"));

		System.out.println("6225768770340329".hashCode()%99);
		System.out.println(Aes.encrypt("6225768770340329"));

		System.out.println("15626174987".hashCode()%99);
		System.out.println(Aes.encrypt("15626174987"));



	}
}
